﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace adminTemplateApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UploadController : ControllerBase
    {
        [HttpPost]
        public async Task<IActionResult> Upload()
        {
            var files = Request.Form.Files;
            // if (files.Count < 1) throw new Exception("无上传图片");
            var dir = AppDomain.CurrentDomain.BaseDirectory+ "upload";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            List<string> fs = new List<string>();
            foreach (var file in files)
            {
                using (var stream = new FileStream(dir + "\\"+file.FileName, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                    fs.Add(file.FileName);
                }
            }
            return Ok(fs);
        }
    }
}
